# Copyright 2013, Michael H. Goldwasser
#
# Developed for use with the book:
#
#    Data Structures and Algorithms in Python
#    Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
#    John Wiley & Sons, 2013
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from .priority_queue_base import PriorityQueueBase
from ..positional_list import PositionalList
from ..exceptions import Empty


class UnsortedPriorityQueue(PriorityQueueBase):  # base class defines _Item
	"""A min-oriented priority queue implemented with an unsorted list."""

	# ----------------------------- nonpublic behavior -----------------------------
	def _find_min(self):
		"""Return Position of item with minimum key."""
		if self.is_empty():  # is_empty inherited from base class
			raise Empty('Priority queue is empty')
		small = self._data.first()
		walk = self._data.after(small)
		while walk is not None:
			if walk.element() < small.element():
				small = walk
			walk = self._data.after(walk)
		return small

	# ------------------------------ public behaviors ------------------------------
	def __init__(self):
		"""Create a new empty Priority Queue."""
		self._data = PositionalList()

	def __len__(self):
		"""Return the number of items in the priority queue."""
		return len(self._data)

	def add(self, key, value):
		"""Add a key-value pair."""
		self._data.add_last(self._Item(key, value))

	def min(self):
		"""Return but do not remove (k,v) tuple with minimum key.

		Raise Empty exception if empty.
		"""
		p = self._find_min()
		item = p.element()
		return (item._key, item._value)

	def remove_min(self):
		"""Remove and return (k,v) tuple with minimum key.

		Raise Empty exception if empty.
		"""
		p = self._find_min()
		item = self._data.delete(p)
		return (item._key, item._value)
